package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.CompetitionUser;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LCompetitionDTO;
import com.learn.core.service.*;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName CompetitionController
 * @date 2021.05.28 10:56
 * @describe 竞赛模块
 */
@Api("竞赛模块")
@RestController
@RequestMapping("/manager/competition")
public class LCompetitionController extends WebController<LCompetition> {

    @Autowired
    private ILCompetitionService ilCompetitionService;
    @Autowired
    private ILCompetitionPaperService ilCompetitionPaperService;
    @Autowired
    private ILCompetitionTargetService ilCompetitionTargetService;
    @Autowired
    private ILCompetitionQuestionService ilCompetitionQuestionService;
    @Autowired
    private ILQuestionsTypeService ilQuestionsTypeService;
    @Autowired
    private ICompetitionUserService iCompetitionUserService;
    @Autowired
    private ILCompetitionUserService ilCompetitionUserService;
    @Autowired
    private ILOperationService ilOperationService;
    @Autowired
    private ILOperationScoreService ilOperationScoreService;
    @Autowired
    private ILCompetitionSortService ilCompetitionSortService;
    @Autowired
    private IUserDetailsGatherService iUserDetailsGatherService;
    @Autowired
    private ILCommentService ilCommentService;
    @Autowired
    ILQuestionsSortService ilQuestionsSortService;
    @Autowired
    ILQuestionsService ilQuestionsService;

    //竞赛模块列表
    @ApiOperation(value = "竞赛模块列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LCompetition> list(@RequestBody LCompetitionDTO lCompetitionDTO){
        try {
            if(lCompetitionDTO==null||lCompetitionDTO.getPage()==null||lCompetitionDTO.getRows()==null){
                return new PageResult<LCompetition>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionDTO.getPage(),lCompetitionDTO.getRows());
            QueryWrapper<LCompetition> queryWrapper = new QueryWrapper<LCompetition>();
            if(StringUtils.isNotEmpty(lCompetitionDTO.getCompetitionSort()))
                queryWrapper.eq("competition_sort",lCompetitionDTO.getCompetitionSort());
            Page<LCompetition> lEvaluates = (Page<LCompetition>) ilCompetitionService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lEvaluates)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LCompetition lCompetition: lEvaluates) {
                QueryWrapper<LCompetitionTarget> targetQueryWrapper = new QueryWrapper<LCompetitionTarget>();
                targetQueryWrapper.eq("competition_id",lCompetition.getCompetitionId());
                LCompetitionTarget lCompetitionTarget = ilCompetitionTargetService.getOne(targetQueryWrapper);
                if(null != lCompetitionTarget)
                    lCompetition.setCompetitionTarget(lCompetitionTarget);
            }
            // 返回
            return new PageResult(lEvaluates.getTotal(), lEvaluates, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新竞赛考次", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LCompetition lCompetition, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lCompetition!=null){
            lCompetition.setUpdateTime(new Date());
            lCompetition.setUpdateUser(userId);
            if(ilCompetitionService.updateById(lCompetition)){
                LCompetitionTarget lCompetitionTarget = lCompetition.getCompetitionTarget();
                ilCompetitionTargetService.updateById(lCompetitionTarget);
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //发布
    @ApiOperation(value = "发布")
    @GetMapping("/release")
    @UserLoginToken
    @Log(title = "发布竞赛考次", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> release(@RequestParam("id")Integer id,@RequestParam("release")String release, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(id!=null){
                LCompetition lCompetition = new LCompetition();
                lCompetition.setCompetitionId(id);
                lCompetition.setUpdateTime(new Date());
                lCompetition.setUpdateUser(userId);
                lCompetition.setReleaseTime(new Date());
                lCompetition.setReleaseState(release);
                if(ilCompetitionService.updateById(lCompetition)){
                    if(!"2".equals(lCompetition.getCompetitionType())){
                        ilCompetitionPaperService.competitionPaper(id,userId);
                    }
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //修改竞赛状态
    @ApiOperation(value = "修改竞赛状态")
    @GetMapping("/editCompetition")
    @UserLoginToken
    @Log(title = "修改竞赛状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> editCompetition(@RequestParam("id")Integer id,@RequestParam("state")String state, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(id!=null){
                LCompetition lCompetition = new LCompetition();
                lCompetition.setCompetitionId(id);
                lCompetition.setUpdateTime(new Date());
                lCompetition.setUpdateUser(userId);
                lCompetition.setCompetitionState(state);
                if(ilCompetitionService.updateById(lCompetition)){
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加竞赛考次", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LCompetition lCompetition, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lCompetition!=null) {
                if (StringUtils.isEmpty(lCompetition.getCompetitionName()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COMPETITION_NAME);
                lCompetition.setCreateTime(new Date());
                lCompetition.setCreateUser(userId);
                lCompetition.setCompetitionState("1");
                if (ilCompetitionService.save(lCompetition)) {
                    LCompetitionTarget lCompetitionTarget = lCompetition.getCompetitionTarget();
                    lCompetitionTarget.setCompetitionId(lCompetition.getCompetitionId());
                    ilCompetitionTargetService.save(lCompetitionTarget);
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除竞赛考次", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LCompetition lCompetition){
        try {
            if(lCompetition!=null){
                if(lCompetition.getCompetitionId()!=null){
                    QueryWrapper<LCompetition> queryWrapper = new QueryWrapper<LCompetition>();
                    queryWrapper.in(true, "competition_id", lCompetition.getCompetitionId());
                    if(ilCompetitionService.remove(queryWrapper)){
                        QueryWrapper<LCompetitionTarget> lCompetitionTargetQueryWrapper = new QueryWrapper<LCompetitionTarget>();
                        lCompetitionTargetQueryWrapper.in(true, "competition_id", lCompetition.getCompetitionId());
                        ilCompetitionTargetService.remove(lCompetitionTargetQueryWrapper);
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增竞赛抽题策略
    @ApiOperation(value = "新增竞赛抽题策略")
    @PostMapping("/eqadd")
    @UserLoginToken
    @Log(title = "新增竞赛抽题策略", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> eqadd(@RequestBody LCompetition lCompetition, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lCompetition!=null) {
                if (StringUtils.isEmpty(lCompetition.getCompetitionId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.LOAD_ERROR);
                }
                // 修改抽题信息
                lCompetition.setUpdateTime(new Date());
                lCompetition.setUpdateUser(userId);
                if(ilCompetitionService.updateById(lCompetition)) {
                    List<LCompetitionQuestion> competitionQuestionList = lCompetition.getCompetitionQuestionList();
                    for (LCompetitionQuestion lCompetitionQuestion : competitionQuestionList) {
                        if (StringUtils.isNotEmpty(lCompetitionQuestion.getCompetitionQuestionId()))
                            ilCompetitionQuestionService.updateById(lCompetitionQuestion);
                        else
                            ilCompetitionQuestionService.save(lCompetitionQuestion);
                    }
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //查询竞赛抽题策略
    @ApiOperation(value = "查询竞赛抽题策略")
    @GetMapping("/eqinfo")
    @UserLoginToken
    public CommonResult<Object> eqinfo(@RequestParam("id")Integer id){
        try {
            QueryWrapper<LCompetitionQuestion> questionQueryWrapper = new QueryWrapper<LCompetitionQuestion>();
            questionQueryWrapper.eq("competition_id",id);
            List<LCompetitionQuestion> list = ilCompetitionQuestionService.list(questionQueryWrapper);
            for (LCompetitionQuestion lCompetitionQuestion: list) {
                LQuestionsType lQuestionsType = ilQuestionsTypeService.getById(lCompetitionQuestion.getCompetitionTypeId());
                if(null != lQuestionsType)
                    lQuestionsType.setQuestionsTypeName(lQuestionsType.getQuestionsTypeName());
            }
            return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,list);
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //删除
    @ApiOperation(value = "删除竞赛抽题策略")
    @PostMapping("/eqdel")
    @UserLoginToken
    @Log(title = "删除竞赛抽题策略", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> eqdel(@RequestBody LCompetitionQuestion lCompetitionQuestion){
        try {
            if(lCompetitionQuestion!=null){
                if(lCompetitionQuestion.getCompetitionQuestionId()!=null){
                    QueryWrapper<LCompetitionQuestion> queryWrapper = new QueryWrapper<LCompetitionQuestion>();
                    queryWrapper.eq("competition_question_id", lCompetitionQuestion.getCompetitionQuestionId());
                    if(ilCompetitionQuestionService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //竞赛排行
    @ApiOperation(value = "竞赛排行")
    @UserLoginToken
    @PostMapping("/ranking")
    public PageResult<CompetitionUser> ranking(@RequestBody LCompetitionDTO lCompetitionDTO){
        try {
            if(lCompetitionDTO==null||lCompetitionDTO.getPage()==null||lCompetitionDTO.getRows()==null){
                return new PageResult<CompetitionUser>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionDTO.getPage(),lCompetitionDTO.getRows());
            QueryWrapper<CompetitionUser> queryWrapper = new QueryWrapper<CompetitionUser>();
            queryWrapper.eq("competition_id",lCompetitionDTO.getCompetitionId());
            if(StringUtils.isNotEmpty(lCompetitionDTO.getUserId())){
                queryWrapper.eq("user_id",lCompetitionDTO.getUserId());
            }
            queryWrapper.orderByDesc("user_score");
            queryWrapper.orderByAsc("competition_user_time");
            Page<CompetitionUser> competitionUsers = (Page<CompetitionUser>) iCompetitionUserService.list(queryWrapper);
            if (CollectionUtils.isEmpty(competitionUsers)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (CompetitionUser competitionUser: competitionUsers) {
                QueryWrapper<UserDetailsGather> userQueryWrapper = new QueryWrapper<UserDetailsGather>();
                userQueryWrapper.eq("user_id",competitionUser.getUserId());
                UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(userQueryWrapper);
                if(null != userDetailsGather)
                    competitionUser.setUserName(userDetailsGather.getName());
            }
            // 返回
            return new PageResult(competitionUsers.getTotal(), competitionUsers, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //排名详情
    @ApiOperation(value = "排名详情")
    @UserLoginToken
    @PostMapping("/rankInfo")
    public PageResult<CompetitionUser> rankInfo(@RequestBody LCompetitionDTO lCompetitionDTO){
        try {
            if(lCompetitionDTO==null||lCompetitionDTO.getPage()==null||lCompetitionDTO.getRows()==null){
                return new PageResult<CompetitionUser>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionDTO.getPage(),lCompetitionDTO.getRows());
            QueryWrapper<CompetitionUser> queryWrapper = new QueryWrapper<CompetitionUser>();
            queryWrapper.eq("user_id",lCompetitionDTO.getUserId());
            if(StringUtils.isNotEmpty(lCompetitionDTO.getStartTime()))
                queryWrapper.gt("release_time",lCompetitionDTO.getStartTime());
            if(StringUtils.isNotEmpty(lCompetitionDTO.getEndTime()))
                queryWrapper.le("release_time",lCompetitionDTO.getEndTime());
            if(StringUtils.isNotEmpty(lCompetitionDTO.getCompetitionSort()))
                queryWrapper.le("competition_sort",lCompetitionDTO.getCompetitionSort());
            queryWrapper.orderByDesc("release_time");
            Page<CompetitionUser> competitionUsers = (Page<CompetitionUser>) iCompetitionUserService.list(queryWrapper);
            if (CollectionUtils.isEmpty(competitionUsers)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(competitionUsers.getTotal(), competitionUsers, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新实操状态
    @ApiOperation(value = "更新实操状态")
    @PostMapping("/editState")
    @UserLoginToken
    @Log(title = "更新实操状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> editState(@RequestParam("id")String competitionUserId) {
        if(competitionUserId!=null){
            String [] id = competitionUserId.split(",");
            for (String str: id) {
                LCompetitionUser lCompetitionUser = new LCompetitionUser();
                lCompetitionUser.setCompetitionUserId(Integer.parseInt(str));
                lCompetitionUser.setPaperState("2");
                if(ilCompetitionUserService.updateById(lCompetitionUser)){
                    LOperation lOperation = new LOperation();
                    lOperation.setCompetitionId(lCompetitionUser.getCompetitionId());
                    lOperation.setOperationUserId(lCompetitionUser.getUserId());
                    ilOperationService.save(lOperation);
                }
            }
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //实操列表
    @ApiOperation(value = "实操列表")
    @UserLoginToken
    @PostMapping("/operationList")
    public PageResult<LOperation> operationList(@RequestBody LCompetitionDTO lCompetitionDTO){
        try {
            if(lCompetitionDTO==null||lCompetitionDTO.getPage()==null||lCompetitionDTO.getRows()==null){
                return new PageResult<LOperation>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionDTO.getPage(),lCompetitionDTO.getRows());
            QueryWrapper<LOperation> queryWrapper = new QueryWrapper<LOperation>();
            queryWrapper.eq("competition_id",lCompetitionDTO.getCompetitionId());
            Page<LOperation> lOperations = (Page<LOperation>) ilOperationService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lOperations)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LOperation lOperation : lOperations) {
                QueryWrapper<LOperationScore> scoreQueryWrapper = new QueryWrapper<LOperationScore>();
                scoreQueryWrapper.eq("operation_id",lOperation.getOperationId());
                List<LOperationScore> list = ilOperationScoreService.list(scoreQueryWrapper);
                lOperation.setOperationScoreList(list);
                int score = 0;
                for (LOperationScore lOperationScore : list) {
                    score += lOperationScore.getOperationScore();
                }
                if(score > 0){
                    score = score / list.size();
                }
                lOperation.setOperationScore(score);
                LCompetition lCompetition = ilCompetitionService.getById(lOperation.getCompetitionId());
                if(null != lCompetition){
                    LCompetitionSort lCompetitionSort = ilCompetitionSortService.getById(lCompetition.getCompetitionSort());
                    if(null != lCompetitionSort)
                        lOperation.setCompetitionSort(lCompetitionSort.getCompetitionSortName());
                }

                QueryWrapper<UserDetailsGather> userQueryWrapper = new QueryWrapper<UserDetailsGather>();
                userQueryWrapper.eq("user_id",lOperation.getOperationUserId());
                UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(userQueryWrapper);
                if(null != userDetailsGather)
                    lOperation.setOperationUserName(userDetailsGather.getName());
                QueryWrapper<LComment> commentQueryWrapper = new QueryWrapper<LComment>();
                commentQueryWrapper.eq("operation_id",lOperation.getOperationId());
                int count = ilCommentService.count(commentQueryWrapper);
                lOperation.setCommentCount(count);
            }
            // 返回
            return new PageResult(lOperations.getTotal(), lOperations, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //评论列表
    @ApiOperation(value = "评论列表")
    @UserLoginToken
    @PostMapping("/commentList")
    public PageResult<LComment> commentList(@RequestBody LCompetitionDTO lCompetitionDTO){
        try {
            if(lCompetitionDTO==null||lCompetitionDTO.getPage()==null||lCompetitionDTO.getRows()==null){
                return new PageResult<LComment>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionDTO.getPage(),lCompetitionDTO.getRows());
            QueryWrapper<LComment> commentQueryWrapper = new QueryWrapper<LComment>();
            commentQueryWrapper.eq("operation_id",lCompetitionDTO.getOperationId());
            Page<LComment> lComments = (Page<LComment>) ilCommentService.list(commentQueryWrapper);
            if (CollectionUtils.isEmpty(lComments)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lComments.getTotal(), lComments, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //删除评论
    @ApiOperation(value = "删除评论")
    @PostMapping("/eqdelComment")
    @UserLoginToken
    @Log(title = "删除评论", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> eqdelComment(@RequestBody LComment lComment){
        try {
            if(lComment!=null){
                if(lComment.getCommentId()!=null){
                    QueryWrapper<LComment> queryWrapper = new QueryWrapper<LComment>();
                    queryWrapper.eq("comment_id", lComment.getCommentId());
                    if(ilCommentService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //查询对应题型分类下题的数量
    @ApiOperation(value = "查询对应题型分类下题的数量")
    @GetMapping("/queryNum")
    @UserLoginToken
    public CommonResult<Object> queryNum(@RequestParam("questionsSortId") Integer questionsSortId,
                                         @RequestParam(value = "knowledge",required = false)String knowledge,
                                         @RequestParam(value = "difficulty",required = false)String difficulty){
        try {
            if(StringUtils.isEmpty(questionsSortId))
                return (CommonResult)ResultUtil.error(ResultEnum.NOT_QUESTIONS_SORT);
            QueryWrapper<LQuestions> queryWrapper = new QueryWrapper<LQuestions>();
            queryWrapper.select("questions_type,count(*) as count");
            // 题库分类下所有题 包括子分类
            LQuestionsSort lQuestionsSort = ilQuestionsSortService.getById(questionsSortId);
            List<Integer> ids = new ArrayList<>();
            ids.add(lQuestionsSort.getQuestionsSortId());
            List<Integer> sortIds = nodeStr(ids);
            queryWrapper.in("questions_sort_id",sortIds);
            if(StringUtils.isNotEmpty(knowledge))
                queryWrapper.like("knowledge",knowledge);
            if (StringUtils.isNotEmpty(difficulty))
                queryWrapper.eq("questions_difficulty",difficulty);
            queryWrapper.groupBy("questions_type");
            List<LQuestions> lQuestionsList = ilQuestionsService.list(queryWrapper);
            for (LQuestions lQuestions: lQuestionsList) {
                LQuestionsType lQuestionsType = ilQuestionsTypeService.getById(lQuestions.getQuestionsType());
                if(null != lQuestionsType)
                    lQuestions.setQuestionsTypeName(lQuestionsType.getQuestionsTypeName());
            }
            return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,lQuestionsList);
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
    }
    //子分类ID
    private List<Integer> nodeStr (List<Integer> lists){
        List<Integer> ids = new ArrayList<>();
        QueryWrapper<LQuestionsSort> queryWrapper = new QueryWrapper<LQuestionsSort>();
        for (int i = 0; i <lists.size() ; i++) {
            int orgid = lists.get(i);

            queryWrapper.eq("parent_node",orgid);
            List<LQuestionsSort> lQuestionsSorts = ilQuestionsSortService.list(queryWrapper);
            for (int j = 0; j < lQuestionsSorts.size(); j++) {
                LQuestionsSort lQuestionsSort = lQuestionsSorts.get(j);
                ids.add(lQuestionsSort.getQuestionsSortId());
            }
        }
        if(ids.size() > 0){
            this.nodeStr(ids);
        }
        ids.addAll(lists);
        return ids;
    }
}
